package com.umeox.watch.moto.gallery.ui;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.watch.moto.gallery.R;
import com.umeox.watch.moto.gallery.db.AppDataBase;
import com.umeox.watch.moto.gallery.db.model.AlbumInfo;

import java.io.File;
import java.util.List;
import java.util.concurrent.Executors;

/**
 * Created by Rambo 2019-11-23
 * Describe:
 */
public class ConfirmDeleteActivity extends ThemeAppCompatActivity {


    private ImageView mConfirmAb;
    private TextView mDoneIv;

    private static final int DELETE_COMPLETE = 0x10;
    private static final int FINISH_ACTIVITY = 0x12;

    private List<String> mPhotos;

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == DELETE_COMPLETE) {
                mDoneIv.setVisibility(View.VISIBLE);
                mHandler.sendEmptyMessageDelayed(FINISH_ACTIVITY, 1000);
            } else if (msg.what == FINISH_ACTIVITY) {
                ConfirmDeleteActivity.this.setResult(RESULT_OK, new Intent());
                finish();
            }
            return false;
        }
    });

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_confirm_delete);
        mPhotos = getIntent().getStringArrayListExtra("list");
        mConfirmAb = findViewById(R.id.ivConfirm);
        mDoneIv = findViewById(R.id.ivDone);


        mConfirmAb.setOnClickListener(v -> {
            //启动线程删除图片
            Executors.newSingleThreadExecutor()
                    .execute(() -> {
                        for (String imgPath : mPhotos) {
                            deleteImage(imgPath);
                        }
                        mHandler.sendEmptyMessage(DELETE_COMPLETE);
                    });
        });

    }

    private void deleteImage(String imgPath) {
        ContentResolver resolver = getContentResolver();
        Cursor cursor = MediaStore.Images.Media.query(resolver, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=?",
                new String[]{imgPath}, null);
        boolean result;
        if (cursor.moveToFirst()) {
            long id = cursor.getLong(0);
            Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            Uri uri = ContentUris.withAppendedId(contentUri, id);
            int count = getContentResolver().delete(uri, null, null);
            result = count == 1;
        } else {
            File file = new File(imgPath);
            result = file.delete();
        }
        cursor.close();
        Logger.i("TAG", "result:" + result);

        if (result) {
            //删除自定义数据库中对应的数据
            AlbumInfo albumInfo = AppDataBase.getInstance().albumInfoDao().queryByImagePath(imgPath);
            if (albumInfo != null) {
                int result1 = AppDataBase.getInstance().albumInfoDao().delete(albumInfo);
                Logger.i("TAG", "result1:" + result1);
            }
        }
    }
}
